#!/bin/bash

echo "================================================"
echo "   日志分析平台微服务 - 一键启动脚本"
echo "================================================"
echo ""

echo "[1/4] 检查Nacos是否运行..."
if ! curl -s http://localhost:8848/nacos > /dev/null 2>&1; then
    echo "[警告] Nacos未运行，请先启动Nacos！"
    echo "启动命令: cd nacos/bin && sh startup.sh -m standalone"
    exit 1
fi
echo "[√] Nacos已运行"

echo ""
echo "[2/4] 编译项目..."
mvn clean package -DskipTests
if [ $? -ne 0 ]; then
    echo "[×] 编译失败！"
    exit 1
fi
echo "[√] 编译成功"

echo ""
echo "[3/4] 启动微服务..."
echo ""

echo "启动日志分析服务 (端口8081)..."
cd log-analysis-service && mvn spring-boot:run &
sleep 30
cd ..

echo "启动日志测试服务 (端口8082)..."
cd log-test-service && mvn spring-boot:run &
sleep 20
cd ..

echo "启动API网关 (端口8080)..."
cd log-analysis-gateway && mvn spring-boot:run &
sleep 20
cd ..

echo ""
echo "[4/4] 启动前端..."
cd frontend && npm run dev &
cd ..

echo ""
echo "================================================"
echo "   所有服务启动完成！"
echo "================================================"
echo ""
echo "访问地址:"
echo "  前端应用: http://localhost:3000"
echo "  API网关: http://localhost:8080"
echo "  Nacos控制台: http://localhost:8848/nacos"
echo ""
echo "默认账号: admin / admin123"
echo ""

